package com.xhh.greynewsapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;
import com.xhh.greynewsapp.adapter.ViewPagerAdapter;
import com.xhh.greynewsapp.fragment.HomeFragment;
import com.xhh.greynewsapp.fragment.MineFragment;
import com.xhh.greynewsapp.fragment.NewsFragment;
import com.xhh.greynewsapp.model.topnews.NewsData;
import com.xhh.greynewsapp.model.topnews.NewsTop;
import com.xhh.greynewsapp.utils.HttpUtils;
import com.xhh.greynewsapp.utils.NoScrollViewPager;

import org.jetbrains.annotations.NotNull;
import org.litepal.LitePal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.my_tb)
    Toolbar toolbar;

    @BindView(R.id.view_pager)
    NoScrollViewPager viewPager;

    @BindView(R.id.navigation)
    BottomNavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this,"518e64e32559d1864c6f40e48a527b2e");

        ButterKnife.bind(this);

        // 导航栏
        setSupportActionBar(toolbar);

        //ViewPager
        initViewPager();

        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_index:
                        viewPager.setCurrentItem(0);
                        toolbar.setTitle("首页");
                        break;
                    case R.id.action_news:
                        viewPager.setCurrentItem(1);
                        toolbar.setTitle("新闻");
                        break;
                    case R.id.action_mine:
                        viewPager.setCurrentItem(2);
                        toolbar.setTitle("我的");
                        break;
                }

                return true;
            }
        });
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new NewsFragment());
        fragments.add(new MineFragment());


        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),0, fragments));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}